﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SuperQuiz.Logic;
using SuperQuiz.Model;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Group Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234229

namespace SuperQuiz
{
    /// <summary>
    /// A page that displays an overview of a single group, including a preview of the items
    /// within the group.
    /// </summary>
    public sealed partial class CategoryPage : SuperQuiz.Common.LayoutAwarePage
    {
        public CategoryPage()
        {
            this.InitializeComponent();
            QuizApplication.Instance.SetupSettingsEvents(this);
        }

        protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (pageState != null && pageState.ContainsKey("CategoryId"))
            {
                navigationParameter = pageState["CategoryId"];
            }
            await QuizApplication.Instance.AwaitLoad();
            var quizCategory = QuizApplication.Instance.GetCategory((String)navigationParameter);
            this.DefaultViewModel["Category"] = quizCategory;
        }

        protected override void SaveState(Dictionary<string, object> pageState)
        {
            var quizCategory = (QuizCategory)this.DefaultViewModel["Category"];
            pageState["CategoryId"] = quizCategory.UniqueId;
        }

        void ItemView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var quiz = (Quiz)e.ClickedItem;
            this.Frame.Navigate(typeof(QuizPage), quiz.UniqueId);
        }
    }
}
